package view;

import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JLabel;

import java.awt.Color;
import java.awt.Font;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JComboBox;
import org.jdom.JDOMException;
import bean.Configuracao;
import util.Constantes;
import util.PersistenciaXml;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import language.Strings;

public class ConfigTreinamento extends JFrame {

	private static final long serialVersionUID = 1L;
	private JPanel jContentPane = null;
	private JLabel lbTitulo = null;
	private JLabel lbNumEscondida = null;
	private JLabel lbEpoch = null;
	private JLabel lbErro = null;
	private JButton btSalvar = null;
	private JButton btCancelar = null;
	private JButton btVerMaximos = null;
	private JComboBox cbEscondida = null;
	private JComboBox cbEpoch = null;
	private JComboBox cbErro = null;

	public ConfigTreinamento() {
		super();
		initialize();
		
		carregaDadosIniciais();
	}

	private void initialize() {
		this.setSize(382, 340);
		this.setContentPane(getJContentPane());
		this.setTitle(Strings.TITULO_TREINAMENTO);
		this.setDefaultCloseOperation(HIDE_ON_CLOSE);
		this.setResizable(false);
		this.setLocation(
						((Toolkit.getDefaultToolkit().getScreenSize().width / 2) - (this
								.getWidth() / 2)),
						((Toolkit.getDefaultToolkit().getScreenSize().height / 2) - (this
								.getHeight() / 2)));
		this.addWindowListener(new WindowAdapter() {
			public void windowClosing (WindowEvent e) {
				Principal.JANELA_ABERTA = false;				
			}			
		});
	}

	private JPanel getJContentPane() {
		if (jContentPane == null) {
			lbTitulo = new JLabel();
			lbTitulo.setBounds(new Rectangle(40, 15, 350, 25));
			lbTitulo.setText(Strings.TITULO_CONFIG_TREINAMENTO);
			lbTitulo.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 15));
			lbTitulo.setForeground(Constantes.COR_AZUL_MARINHO);
			
			lbErro = new JLabel();
			lbErro.setBounds(new Rectangle(195, 130, 73, 15));
			lbErro.setText(Strings.TEXT_LABEL_ERRO + " (%):");
			lbEpoch = new JLabel();
			lbEpoch.setBounds(new Rectangle(17, 130, 63, 15));
			lbEpoch.setText(Strings.TEXT_LABEL_EPOCH + " :");
			lbNumEscondida = new JLabel();
			lbNumEscondida.setBounds(new Rectangle(17, 80, 253, 15));
			lbNumEscondida.setText(Strings.TEXT_LABEL_CAMADA_ESCONDIDA + " :");
			
			jContentPane = new JPanel();
			jContentPane.setLayout(null);
			jContentPane.add(lbTitulo, null);
			jContentPane.add(lbNumEscondida, null);
			jContentPane.add(lbEpoch, null);
			jContentPane.add(lbErro, null);
			jContentPane.add(getBtSalvar(), null);
			jContentPane.add(getBtCancelar(), null);
			jContentPane.add(getBtVerMaximos(), null);
			jContentPane.add(getCbEscondida(), null);
			jContentPane.add(getCbEpoch(), null);
			jContentPane.add(getCbErro(), null);

			jContentPane.setBackground(Color.WHITE);
		}
		return jContentPane;
	}

	private JButton getBtSalvar() {
		if (btSalvar == null) {
			btSalvar = new JButton();
			btSalvar.setBounds(new Rectangle(55, 260, 110, 25));
			btSalvar.setText(Strings.TEXT_BT_SALVAR);
			btSalvar.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					String escondida = getCbEscondida().getSelectedItem().toString();
					String epoch = getCbEpoch().getSelectedItem().toString();
					String erro = getCbErro().getSelectedItem().toString();
					
					Configuracao config = new Configuracao();
					config.setQtdNeuroniosCamadaEscondida(escondida);
					config.setEpoch(epoch);
					config.setErro(erro);
					
					PersistenciaXml persistenciaXml = new PersistenciaXml();
					persistenciaXml.salvaConfiguracoes(config);
				}
			});
		}
		return btSalvar;
	}

	private JButton getBtCancelar() {
		if (btCancelar == null) {
			btCancelar = new JButton();
			btCancelar.setBounds(new Rectangle(225, 260, 110, 25));
			btCancelar.setText(Strings.TEXT_BT_CANCELAR);
			btCancelar.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					carregaDados();
				}
			});
		}
		return btCancelar;
	}

	private JButton getBtVerMaximos() {
		if (btVerMaximos == null) {
			btVerMaximos = new JButton();
			btVerMaximos.setBounds(new Rectangle(130, 200, 130, 25));
			btVerMaximos.setText(Strings.TEXT_BT_VER_MAXIMOS);
			btVerMaximos.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					StartMaximos startMaximos = new StartMaximos();
					
					startMaximos.setVisible(true);
					startMaximos.setState(JFrame.NORMAL);
				}
			});
		}
		return btVerMaximos;
	}
	
	private JComboBox getCbEscondida() {
		if (cbEscondida == null) {
			cbEscondida = new JComboBox();
			cbEscondida.setBounds(new Rectangle(280, 80, 70, 24));
		}
		return cbEscondida;
	}

	private JComboBox getCbEpoch() {
		if (cbEpoch == null) {
			cbEpoch = new JComboBox();
			cbEpoch.setBounds(new Rectangle(100, 130, 70, 24));
		}
		return cbEpoch;
	}

	private JComboBox getCbErro() {
		if (cbErro == null) {
			cbErro = new JComboBox();
			cbErro.setBounds(new Rectangle(280, 130, 70, 24));
		}
		return cbErro;
	}
	
	private void carregaDadosIniciais(){
		preencheCbEscondida();
		preencheCbEpoch();
		preencheCbErro();
		
		carregaDados();
	}
	
	private void carregaDados(){		
		PersistenciaXml persistenciaXml = new PersistenciaXml();
		try {
			Configuracao config = persistenciaXml.selectConfiguracoesXML();
			
			String esc = config.getQtdNeuroniosCamadaEscondida();
			for (int i = 0; i < getCbEscondida().getItemCount(); i++) {
				if(esc.equalsIgnoreCase(getCbEscondida().getItemAt(i).toString())){
					getCbEscondida().setSelectedIndex(i);
				}
			}
			
			String epoch = config.getEpoch();
			for (int i = 0; i < getCbEpoch().getItemCount(); i++) {
				if(epoch.equalsIgnoreCase(getCbEpoch().getItemAt(i).toString())){
					getCbEpoch().setSelectedIndex(i);
				}
			}
			
			String erro = config.getErro();
			for (int i = 0; i < getCbErro().getItemCount(); i++) {
				if(erro.equalsIgnoreCase(getCbErro().getItemAt(i).toString())){
					getCbErro().setSelectedIndex(i);
				}
			}			
		} catch (JDOMException e) {
			e.printStackTrace();
		} catch (IOException e) {
			System.err.println("Arquivo de Configuracao nao pode ser encontrado!");
			e.printStackTrace();
		}
	}

	private void preencheCbEscondida(){
		getCbEscondida().removeAllItems();
		
		for (int i = Constantes.INPUT_COUNT; i <= 50; i++) {
			getCbEscondida().addItem(i);
		}
	}
	
	private void preencheCbEpoch(){
		getCbEpoch().removeAllItems();
		
		int valor = 500;
		for (int i = 0; i < 1000; i++) {
			getCbEpoch().addItem(valor);
			valor += 500;
		}
	}
	
	private void preencheCbErro(){
		getCbErro().removeAllItems();
		
		for (int i = 1; i <= 100; i++) {
			getCbErro().addItem(i);
		}
	}
}  //  @jve:decl-index=0:visual-constraint="10,10"
